﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using AppGPS.Helper;
using Windows.Networking.Connectivity;
using Models;
using System.Threading.Tasks;
using AppGPS.ViewModels;
using System.Text.RegularExpressions;

namespace AppGPS.Views.UControl
{
    public partial class CreateInfomation : UserControl
    {
        UserInfoRequest _userRequest = new UserInfoRequest(App.dataShare.szHost);
        PhotoChooserTask photoChooserTask;
        private string _szAvatar;
        public string Fullname
        {
            set { txtFullname.Text = value; }
            get { return txtFullname.Text; }
        }
        public string Email
        {
            set { txtEmailAdress.Text = value; }
            get { return txtEmailAdress.Text; }
        }
        public string Phone
        {
            set { txtPhoneNumber.Text = value; }
            get { return txtPhoneNumber.Text; }
        }
        public string IP
        {
            get { return Utils.GetIP(); }
        }
        public string Avatar
        {
            get { return _szAvatar; }
        }
        public UserInfoResult Data
        {
            get
            {
                UserInfoResult Result = new UserInfoResult();
                Result.Fullname = Fullname;
                Result.Email = Email;
                Result.IP = IP;
                Result.Phone = Phone;
                Result.Avatar = Avatar;
                Result.Latitude = 0.0;
                Result.Longtitude = 0.0;
                return Result;
            }
        }
       
        public CreateInfomation()
        {
            InitializeComponent();
            photoChooserTask = new PhotoChooserTask();
            photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
        }
        public async Task<bool> EmailCompare()
        {
            if (!Utils.CheckEmail(txtEmailAdress.Text)) return false;
            return await _userRequest.EmailExists(txtEmailAdress.Text);
        }
        private void txtAvatar_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
             photoChooserTask.Show();            
        }
        private void photoChooserTask_Completed(object sender, PhotoResult e)
        {
            if (e.ChosenPhoto == null) return;

            BitmapImage image = new BitmapImage();

            image.SetSource(e.ChosenPhoto);

            _szAvatar = ImageConvert.BitmapImageToBase64(image, 100, 100);

            txtAvatar.Text = e.OriginalFileName;

            ImageAvatar.Source = ImageConvert.base64image(_szAvatar);
        }
    }
}
